﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BrumbaZDuplicateFileFinder
{
    public partial class Form1 : Form
    {

        List<FileInformation> totalFiles = new List<FileInformation>();
        List<FileInformation> repeatedFiles = new List<FileInformation>();

        delegate void ProcessFinish();
        delegate void FillRepeated(FileInformation file);
        delegate void FillTotal(FileInformation file);

        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            autoEvent = new System.Threading.AutoResetEvent(false);
            button3_Click(null, null);
            System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(this.textBox1.Text);
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(getFilesCaller));
            thread.Start(dirInfo);
        }

        private void finish()
        {
            this.dataGridView1.DataSource = null;
            this.dataGridView1.DataSource = repeatedFiles;

        }
        void getFilesCaller(object dirInfo)
        {

            getFiles((System.IO.DirectoryInfo)dirInfo);
            this.BeginInvoke(new ProcessFinish(finish));
        }

        System.Threading.AutoResetEvent autoEvent;

        private void getFiles(System.IO.DirectoryInfo dirInfo)
        {
            try
            {
                System.IO.DirectoryInfo[] dir = dirInfo.GetDirectories();
                System.IO.FileInfo[] files = dirInfo.GetFiles();
                for (int iFiles = 0; iFiles < files.Length; iFiles++)
                {
                    IEnumerable<FileInformation> filesToMark = totalFiles.Where(fileTotal => fileTotal.Name.IndexOf(files[iFiles].Name) > -1);
                    FileInformation file = new FileInformation();
                    file.Name = files[iFiles].Name;
                    file.Size = files[iFiles].Length;
                    file.Path = files[iFiles].FullName;
                    if (filesToMark.ToList().Count > 0)
                    {
                        this.BeginInvoke(new FillRepeated(fillRepeated), file);
                        autoEvent.WaitOne();

                    }
                    else
                    {
                        this.BeginInvoke(new FillTotal(fillTotal), file);
                        autoEvent.WaitOne();
                    }
                    System.Threading.Thread.Sleep(10);


                }
                if (dir.Length > 0)
                    for (int iDir = 0; iDir < dir.Length; iDir++)
                        getFiles(dir[iDir]);

            }
            catch (System.UnauthorizedAccessException ex)
            {
                //TODO: Log Folders with this exception
            }

        }

        private void fillRepeated(FileInformation file)
        {
            this.repeatedFiles.Add(file);
            autoEvent.Set();
        }

        private void fillTotal(FileInformation file)
        {
            this.label1.Text = file.Path;
            this.totalFiles.Add(file);
            autoEvent.Set();
            
        }

        class FileInformation
        {
            public string Name
            {
                get;
                set;
            }

            public string Path
            {
                get;
                set;
            }

            public long Size
            {
                get;
                set;
            }
            public DateTime Modified
            {
                get;
                set;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            textBox1.Text = folderBrowserDialog1.SelectedPath;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = null;
            this.repeatedFiles.Clear();
            this.totalFiles.Clear();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            for (int iSelected = 0; iSelected < dataGridView1.SelectedRows.Count; iSelected++)
            {
                if (System.IO.File.Exists(dataGridView1.SelectedRows[iSelected].Cells[1].Value.ToString()))
                    System.IO.File.Delete(dataGridView1.SelectedRows[iSelected].Cells[1].Value.ToString());
            }
            MessageBox.Show("Search again if you want! But Clear first!");
        }
    }
}
